<template>
    <div class="shopcar-container">
        <div class="goods-list">
            <!-- 商品列表区域 -->
            <div class="mui-card" v-for="(item,index) in shopcarinfo" :key="item.id">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <mt-switch
                            v-model="$store.getters.getGoodsSelected[item.id]"
                            @change="SelectedChanged(item.id,$store.getters.getGoodsSelected[item.id])">
                        </mt-switch>
                        <img :src="item.thumb_path" alt="">
                        <div class="info">
                            <h1>{{ item.title }}</h1>
                            <p>
                                <span>￥{{ item.sell_price}}</span>

                                <numbox :initcount = "$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                                <!-- 问题：如何从cart中获取商品的数量 -->
                                <!-- 我们可以先创建一个空对象，然后循环cart中所有商品的数据，把当前循环的商品id作为对象的属性名，count做为对象的属性值，这样，当把所有商品循环一遍，就会得到一个对象{87:2,89:3,90:1,...} -->

                                <a href="" @click.prevent="delGoodsInfo(item.id,index)">删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 结算区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan">
                    <div class="left">
                        <p>总计(不含运费)</p>
                        <p>已选购商品<span class="red">{{ $store.getters.getGoodsCountAndAmount.count}}</span>件，总价￥<span class="red">{{ $store.getters.getGoodsCountAndAmount.amount}}</span></p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>

                </div>
            </div>
        </div>
    </div>
</template>


<script>
import numbox from '../subcomponents/shopcar_numbox.vue'
export default {
    data(){
        return{
            shopcarinfo:[]//购物车中所有商品的数据
        }
    },
    created(){
        this.getShopcarList();
    },
    methods:{
        getShopcarList(){
            //1. 获取store中所有商品的id，然后拼接出一个用逗号分隔的字符串
            var idArr = [];
            this.$store.state.cart.forEach(item => idArr.push(item.id));

            //如果购物车中没有商品，则直接返回，不需要发起请求，否则会报错
            if(idArr.length <= 0){
                return;
            }
            this.axios
            .get('api/goods/getshopcarlist/' + idArr.join(','))
            .then(result=>{
                if(result.data.status ===0){
                    this.shopcarinfo = result.data.message;
                }
            });
        },
        delGoodsInfo(id,index){
            //根据id删除cart中对应的商品，根据index删除组件中shopcarinfo对应的商品信息
            this.shopcarinfo.splice(index,1)
            this.$store.commit('delGoodsInfo',id);
        },
        SelectedChanged(id,value){
            // console.log('id:'+id + '----状态：'+ value);
            this.$store.commit('updateGoodsSelected',
                {
                    id:id,
                    value:value
                }
            );
        }

    },
    components:{
        numbox
    }
}
</script>

<style lang='scss' scoped="">
.shopcar-container{
    background-color: #eee;
    overflow: hidden;
    .goods-list{
        .mui-card-content-inner{
            display:flex;
            align-items:center;
        }
        img{
            width:60px;
            height:60px;
        }
        h1{
            font-size:13px;
        }
        .info{
            display:flex;
            flex-direction:column;
            justify-content:space-between;
            .price{
                color:red;
                font-weight:bold;
            }
        }
    }

    .jiesuan{
        display:flex;
        justify-content:space-between;
        align-items:center;
        .red{
            color:red;
            font-weight:bold;
            font-size:16px;
        }
    }
}
</style>